import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.GridBagConstraints;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class EditStationPanel extends JPanel {
	public static final String NEW = "Tao moi ...";  //  @jve:decl-index=0:
	
	private static final long serialVersionUID = 1L;
	private AutoCompletePanel allStation = null;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private AutoCompletePanel allZonePanel = null;
	private JPanel jPanel2 = null;
	private JLabel jLabel = null;
	private JTextField nameTextField = null;
	private JButton updateButton = null;

	private JButton deleteButton = null;

	/**
	 * This is the default constructor
	 */
	public EditStationPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.setRows(1);
		gridLayout.setColumns(3);
		this.setLayout(gridLayout);
		this.setSize(300, 200);
		this.add(getAllStation(), null);
		this.add(new JPanel(), null);
		this.add(getJPanel(), null);
	}

	public void initAllStationPanel() {
		ArrayList<Item> items = new ArrayList<Item>();
		StationManager sm = null;
		try {
			sm = StationManager.getInstance();
		} catch (SQLException e) {
		}
		ArrayList<Station> stations = sm.getAllStation();
		items.add(new Item(0,NEW));
		for (Station station : stations) {
			Item item = new Item();
			item.id = station.getId();
			item.name = station.getName();
			items.add(item);
		}
		//tab Tim duong
		allStation.setItems(items);
		allStation.updateItemList();
	}
	
	/**
	 * This method initializes allStation	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private AutoCompletePanel getAllStation() {
		if (allStation == null) {
			allStation = new AutoCompletePanel();
			allStation.setLabel("Chọn bến: ");
			
			initAllStationPanel();
			allStation.getItemList().addListSelectionListener(new ListSelectionListener() {
				
				@Override
				public void valueChanged(ListSelectionEvent e) {
					Item item = getAllStation().getSelectedItem();
					if (item == null) return;
					if (getAllStation().getSelectedItem().name.equals(NEW)) {
						getNameTextField().setText("");
						//getAllZonePanel().setItems(new ArrayList<Item>());
					} else {
						getNameTextField().setText(item.name);
						
						ArrayList<Item> items = new ArrayList<Item>();
						ZoneManager zm = null;
						try {
							zm = ZoneManager.getInstance();
						} catch (SQLException e1) {
						}
						ArrayList<Zone> zones = zm.getAllZone();
						
						int selectedIndex = 0;
						Item item2;
						for (Zone zone : zones) {
							item2 = new Item();
							item2.id = zone.getId();
							item2.name = zone.getName();
							items.add(item2);
						}
						//getAllZonePanel().setItems(items);
						//getAllZonePanel().updateItemList();
						
						
						StationManager sm = null;
						try {
							sm = StationManager.getInstance();
						} catch (SQLException e1) {
						}
						Station station = sm.getStation(item.id);
						for (int i = 0; i < items.size(); i++) {
							item2 = items.get(i);
							if (item2.id == station.getZoneId()) {
								getAllZonePanel().getItemList().setSelectedIndex(i);
								break;
							}
						}
					}
				}
			});
		}
		return allStation;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout());
			jPanel.add(getJPanel1(), BorderLayout.NORTH);
			jPanel.add(getAllZonePanel(), BorderLayout.CENTER);
			jPanel.add(getJPanel2(), BorderLayout.SOUTH);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setLayout(new BorderLayout());
			jLabel = new JLabel("Tên:        ");
			jPanel1.add(jLabel, BorderLayout.WEST);
			jPanel1.add(getNameTextField(), BorderLayout.CENTER);
		}
		return jPanel1;
	}

	/**
	 * This method initializes allZonePanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private AutoCompletePanel getAllZonePanel() {
		if (allZonePanel == null) {
			allZonePanel = new AutoCompletePanel();
			allZonePanel.setLabel("Chọn khu vực: ");
			
			ArrayList<Item> items = new ArrayList<Item>();
			ZoneManager zm = null;
			try {
				zm = ZoneManager.getInstance();
			} catch (SQLException e) {
			}
			ArrayList<Zone> zones = zm.getAllZone();
			for (Zone station : zones) {
				Item item = new Item();
				item.id = station.getId();
				item.name = station.getName();
				items.add(item);
			}
			//tab Tim duong
			allZonePanel.setItems(items);
			allZonePanel.updateItemList();
		}
		return allZonePanel;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jPanel2 = new JPanel();
			jPanel2.setLayout(new BorderLayout());
			jPanel2.add(getUpdateButton(), BorderLayout.EAST);
			jPanel2.add(getDeleteButton(), BorderLayout.WEST);
		}
		return jPanel2;
	}

	/**
	 * This method initializes nameTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNameTextField() {
		if (nameTextField == null) {
			nameTextField = new JTextField();
		}
		return nameTextField;
	}

	/**
	 * This method initializes updateButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getUpdateButton() {
		if (updateButton == null) {
			updateButton = new JButton("Cập nhât");
			
			updateButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					Item item = getAllStation().getSelectedItem();
					if (getNameTextField().getText().equals("")) {
						JOptionPane.showMessageDialog(null, "Chưa nhập tên");
						return;
					}
					if (getAllZonePanel().getSelectedItem() == null) {
						JOptionPane.showMessageDialog(null, "Chưa chọn vùng");
						return;
					}
					StationManager sm = null;
					try {
						sm = StationManager.getInstance();
					} catch (SQLException e1) {
					}
					
					boolean result = false;
					if (item == null || item.name.equals(NEW)) {
						result = sm.addStation(new Station(getNameTextField().getText()), allZonePanel.getSelectedItem().id);
					} else {
						result = sm.updateStation(item.id, new Station(getNameTextField().getText()), allZonePanel.getSelectedItem().id);
					}
					if (!result) {
						JOptionPane.showMessageDialog(null, "Hãy chọn tên khác.");
					}
					initAllStationPanel();
				}
			});
		}
		return updateButton;
	}

	/**
	 * This method initializes deleteButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getDeleteButton() {
		if (deleteButton == null) {
			deleteButton = new JButton("Xoá");
			deleteButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					Item item = getAllStation().getSelectedItem();
					if (item == null) return;
					StationManager sm = null;
					try {
						sm = StationManager.getInstance();
						sm.removeBusLine(item.id);
					} catch (SQLException e1) {
					}
					initAllStationPanel();
				}
			});
		}
		return deleteButton;
	}

}
